import axios from 'axios';
import Qs from 'qs';

axios.defaults.timeout = 14000; // 请求超时时间
axios.defaults.withCredentials = true; //默认携带cookie

//配置开发环境 与 生产环境
if(process.env.NODE_ENV == 'development') {
	axios.defaults.requestUrl = '/api/api';
} else if(process.env.NODE_ENV == 'production') {
	axios.defaults.baseURL = window.location.origin + '/api';
}

// http请求拦截器
axios.interceptors.request.use(config => {
	return config;
}, error => {
	return Promise.reject(error);
});

// http响应拦截器
axios.interceptors.response.use(data => {
	return data;
}, error => {
	return Promise.reject(error);
});

export default async(type = 'get', url = '', data = {}, contentType = '', ) => {
	let params = {};
	let headers = {
		'Content-Type': 'application/json;charse=UTF-8', //默认编码方式
	}
	if(type === 'get') {
		Object.keys(data).forEach(key => {
			params[key] = data[key];
		});
	}

	if(type === 'post') {};

	let requestConfig = {
		url: url,
		method: type,
		params: params,
		data: data,
		responseType: 'json',
		headers: headers
	}

	try {
		const response = await axios(url, requestConfig);
		return response.data;
	} catch(error) {
		throw new Error(error);
	}
};

/**
 * Content-Type常用参数
 * application/json;charse=UTF-8
 * application/x-www-form-urlencoded
 * multipart/form-data
 */